#include "data_manager.h"

#ifndef HEAD_H
#include "../head.h"
#endif

const QString Data_Manager::dbName = "galtoabDa_l"; // global_Data
const QString Data_Manager::dbmap = "Dpaatma",dbfacility = "afatcialiDty"; // mapData facilityDate
Data_Manager::Data_Manager() {
    static std::vector<Map_Item> map_items{
        Map_Item('o',1.24),
        Map_Item('r',1.32),
        Map_Item('s',1.26),
        Map_Item('w',1.38),
        Map_Item('A',1.02),
        Map_Item('B',1.23),
        Map_Item('D',1.14),
    };
    map_generator = new MapGenerator(
        grid::WIDTH,
        grid::HEIGHT,
        map_items
        );
}
void Data_Manager::doSave(){
    QSettings setting(dbName,QSettings::IniFormat);
    setting.setValue("game",has_game);
    setting.setValue("gold_number",gold_number);
    setting.beginGroup("progress");
    {
        setting.setValue("grid",grid_progress);
        setting.setValue("hub",hub_progress);
        setting.setValue("interest",interest_rate_progress);
    }
    setting.endGroup();
    if(has_game){
        QSettings map_set(dbmap,QSettings::IniFormat);
        auto & grid_map = map_generator->getGrid();
        for(int i = 0; i < grid::WIDTH; ++i){
            for(int j = 0; j < grid::HEIGHT; ++j){
                map_set.setValue(QString("%1w%2").arg(i).arg(j),grid_map[i][j]);
            }
        }
    }else{

    }
}

void Data_Manager::loadSave(){
    QSettings setting(dbName,QSettings::IniFormat);
    has_game = setting.value("game",false).toBool();
    gold_number = setting.value("gold_number",0).toInt();
    setting.beginGroup("progress");
    {
        grid_progress = setting.value("grid",0).toInt();
        hub_progress = setting.value("hub",0).toInt();
        interest_rate_progress = setting.value("interest",0).toInt();
    }
    setting.endGroup();
    if(has_game){
        QSettings map_set(dbmap,QSettings::IniFormat);
        auto & grid_map = map_generator->getGrid();
        for(int i = 0; i < grid::WIDTH; ++i){
            for(int j = 0; j < grid::HEIGHT; ++j){
                grid_map[i][j] = map_set.value(QString("%1w%2").arg(i).arg(j),'_').toInt();
            }
        }
    }else{
        map_generator->generateMap();
    }
}

